<?php
/*
 * @分销商城
 * @功能说明：订单主业务
 * @更新说明：暂无更新
 * @文件名 OrderController.class.php
 * @编码 UTF-8
 * @创建时间 2016-1-23 下午1:48:30
 * @创建人 XING
 */
namespace hb\Controller;
use Think\Controller;
class OrderController extends ApibaseController {
	/*
	 * 分销商城_订单主页NEW
	 * @date: 2015-12-25 下午4:49:04
	 * @editor: XING
	 */
	public function index(){
		/*参数初始化*/
		$page = I('get.page') ?: 1;
		$perpage = 10;
		$url = $output = $outtype = array();
		//获取订单状态等信息 根据订单号获取商品ID 根据商品ID获取商品来源等信息
		$sql = "SELECT DISTINCT(a.order_id),a.order_status,a.nickname,a.receive_name,a.receive_mobile,a.receive_address,a.order_pay,a.add_at
				FROM fx_order_entity a
				LEFT JOIN fx_order_collect b
				ON a.order_id=b.order_id
				LEFT JOIN fx_goods_entity c
				ON b.goods_id=c.goods_id
				WHERE a.order_type=0 AND c.resource=0";
		if(I('get.order_id')){
			$sql = $sql." AND a.order_id ='".I('get.order_id')."'";
			$url['order_id'] = I('get.order_id');
		}
		if(I('get.cust_name')){
			$sql = $sql." AND a.nickname LIKE '%".I('get.cust_name')."%'";
			$url['cust_name'] = I('get.cust_name');
		}
		if(I('get.cust_mobile')){
			$sql = $sql." AND a.receive_mobile =".I('get.cust_mobile');
			$url['cust_mobile'] = I('get.cust_mobile');
		}
		if((I('get.st') > -2 && isset($_GET['st'])) && !in_array(I('get.st'),array('all','refund'))){
			$sql = $sql." AND a.order_status=".I('get.st');
			$url['st'] = I('get.st');
		}elseif(I('get.st') == 'refund'){
			$sql = $sql." AND a.order_status IN(5,6,7,8,9)";
			$url['st'] = I('get.st');
		}
		$list = CQuery($sql." order by a.add_at DESC limit ".($page-1)*$perpage.",".$perpage);
		$this->assign('page',setPage(count(CQuery($sql)),$perpage,$page,'?'.http_build_query($url)));
		$this->assign('getData',$list);
		$this->display('order/index');
	}


	/*
	 * 分销商城_退货理由
	 * @date: 2016-04-08 18:52:00
	 * @editor: WENCUN
	 * */
	public function refundly(){
		$custData = M('OrderEntity')
			->where(array('order_id'=>I('get.oid')))->find();
		$refunds = explode(',',getSysConfig('refundType'));
		$refundsImgs = explode(',',$custData['refund_imgs']);
		foreach($refundsImgs as $key=>$value){
			$refundsImgs[$key] = transUrl($value);
		}
		$this->refundsImgs = $refundsImgs;
		$this->assign('refunds',$refunds);
		$this->assign('sData',$custData);
		$this->display('order/refundly');
	}

	/*
	 * 分销商城_退货物流信息
	 * @date: 2016-04-12 09:25:00
	 * @editor: WENCUN
	 * */
	public function refundwl(){
		$custData = M('OrderEntity')
			->where(array('order_id'=>I('get.oid')))->find();
		$this->assign('sData',$custData);
		$this->display('order/refundwl');
	}
	
	/*
	 * 分销商城_订单发货
	 * @date: 2016-04-27 11:52:00
	 * @editor: WENCUN
	 */
	public function send(){
		if(I('post.dosubmit') && I('post.id')){
			$sql = "SELECT b.goods_id,b.pay_nums
					FROM fx_order_entity a
					LEFT JOIN fx_order_collect b
					ON a.order_id=b.order_id
					LEFT JOIN  fx_goods_entity c
					ON b.goods_id=c.goods_id
					WHERE a.order_type=0 AND a.order_id=".I('post.id')." AND c.resource=2";
			$goodsId = CQuery($sql);
			foreach($goodsId as $key=>$value){
				$setExpress = M('OrderCollect')->where(array('order_id'=>I('post.id'),'goods_id'=>$value['goods_id']))->setField(array(
					'express_name' => I('post.express_name'),
					'express_num' => I('post.express'),
					'express_at' => time()
				));
				$setKcun = M('GoodsBjie')->where(array('goods_id'=>$value['goods_id']))->setDec('kcun',$value['pay_nums']);
			}
			if($setExpress && $setKcun){
				//根据订单商品物流信息更改订单状态
				$status=1;
				$orderExpress = M('OrderCollect')->field(array('express_num'))->where(array('order_id'=>I('post.id')))->select();
				foreach($orderExpress as $key=>$value){
					if($value['express_num'] == ''){
						$status = 0;
					}
				}
				if($status!=0){
					//更变订单状态
					M('OrderEntity')->where(array('order_id'=>I('post.id')))->setField(array('order_status'=>2));
				}
				$this->showtips('发货成功');
			}else{
				$this->showtips('操作失败');
			}
		}elseif(I('get.oid')){
			$oData = M('OrderEntity')
					  ->where(array('order_id'=>I('get.oid')))->find();
			if($oData['order_status'] != '1'){
				$this->assign('wmsg','无效订单号');
			}
			$this->express = explode(',',getSysConfig('expressComp'));
			$this->assign('sData',$oData);
			$this->display('order/send');
		}
	}


	/*
	 * 分销_导出订单
	 * @date: 2016-04-26 16:17:00
	 * @editor: WENCUN
	 * */
	public function exportExcel(){
		if(IS_POST){
			if(I('post.dosubmit')){
				$tableHeader = array(
					'订单编号','收货人','手机号码','商品ID','商品名称','商品规格','商品数量','省份','城市','详细收货地址','订单成交时间','物流公司','物流运单号','发货仓库','发货时间'
				);
				if(I('post.dotime') && I('post.endtime')){
					$doTime = strtotime(I('post.dotime'));
					$endTime = strtotime(I('post.endtime'));
					$shipments = M('OrderEntity')
						->field(array(
							'order_id','receive_name','receive_mobile','receive_provice','receive_city','receive_address','add_at'
						))
						->where(array('order_status'=>'1','order_type'=>'0'))
						->where('add_at > '.$doTime.' and add_at < '.$endTime)->select();
				}
				//根据订单编号获取订单商品信息(商品标题,商品数量,商品规格)
				foreach($shipments as $key=>$value){
					$sql = "SELECT a.goods_id,a.goods_title,a.goods_attr,a.pay_nums
								FROM fx_order_collect a
								LEFT JOIN fx_goods_entity b
								ON a.goods_id=b.goods_id
								WHERE b.resource=0 AND a.order_id=".$value['order_id'];
					$goodsMsg = CQuery($sql);
					foreach($goodsMsg as $k=>$v){
						$data[] = array(
							'1'=>strval($value['order_id']),
							'2'=>$value['receive_name'],
							'3'=>$value['receive_mobile'],
							'4'=>$v['goods_id'],
							'5'=>$v['goods_title'],
							'6'=>$v['goods_attr'],
							'7'=>$v['pay_nums'],
							'8'=>$value['receive_provice'],
							'9'=>$value['receive_city'],
							'10'=>$value['receive_address'],
							'11'=>date('Y/m/d H:i:s',$value['add_at'])
						);
					}
				};
				if(!$goodsMsg){
					$this->showtips('没有数据');
					exit;
				}
				exportExcel($tableHeader,$data);
			}
		}
		$this->display('order/exportexcel');
	}

	/*
	 * 分销商城_导入Excel表格
	 * @date: 2016-04-27 17:31:00
	 * @editor: WENCUN
	 * */
	public function importExcel(){
		if($_POST){
			//$url = DIRROOT.'/statics'.'/upload/image/20160428/20160428080010_23634.xls';
			$url = DIRROOT.'/statics'.I('post.xls_url');
			$data = importExcel($url);
			$param = array();
			if($data){
				foreach($data as $key=>$value){
					$orderStatus = M('OrderEntity')->where(array('order_id'=>$value['订单编号'],'order_status'=>'1','order_type'=>'0'))->count();
					if($value['物流运单号'] && $orderStatus>0){
						$param[] = array(
							'order_id' => $value['订单编号'],
							'goods_id' => $value['商品ID'],
							'express_name' => $value['物流公司'],
							'express_num' => $value['物流运单号'],
						);
					}
				}
			}
			if($param){
				foreach($param as $key=>$value){
					if($value['express_num']){
						$goodsId = M('GoodsEntity')->where(array('goods_id'=>$value['goods_id'],'resource'=>'0'))->count();
						if($goodsId>0){
							$setExpress = M('OrderCollect')->where(array('order_id'=>$value['order_id'],'goods_id'=>$value['goods_id']))->setField(array(
								'express_name' => $value['express_name'],
								'express_num' => $value['express_num'],
								'express_at' => time()
							));
							if($param[$key]['order_id'] != $param[$key+1]['order_id']){
								//根据订单商品物流信息更改订单状态
								$orderExpress = M('OrderCollect')
									->field('express_num')->where(array('order_id'=>$value['order_id'],'express_at'=>'0'))->count();
								if(!$orderExpress){
									//更变订单状态
									M('OrderEntity')
										->where(array('order_id' => $value['order_id'],'order_status'=>'1'))->setField(array(
											'order_status' => 2,
											'update_at' => time()
										));
								}
							}
						}
					}
				}
				if($setExpress){
					$this->showtips('导入更新订单物流成功');
				}else{
					$this->showtips('操作失败');
				}
			}else{
				$this->showtips('数据有误');
			}
		}
		$this->display('/order/importexcel');
	}


}